package com.system.healthproject.controller;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.bean.ResultDoctor;
import com.system.healthproject.model.admin.paramsbean.PatientParems;
import com.system.healthproject.model.admin.service.AdminPatientService;
import com.system.healthproject.model.admin.service.DoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Api(value = "excel导出",tags = "公共-excel导出API")
@Controller
@RequestMapping("/excel")
@Validated
public class ExcelController {
    @Autowired
    AdminPatientService adminPatientService;
    @Autowired
    DoctorService doctorService;

    @ApiOperation(value = "后端导出患者列表",httpMethod = "get")
    @RequestMapping(value = "/exporPatientList",method = RequestMethod.GET)
    public void exporPatientList(HttpServletResponse response,
                             @NotNull(message = "page不能为空") Integer page,
                             @NotNull(message = "size不能为空") Integer size,
                             @RequestParam(name = "name",required = false) String name,
                             Integer sex,
                             Integer city,
                             Integer doctorId,
                             Integer hasRecord,
                             String  diagnosis_time) throws IOException {
        ExcelWriter writer = null;
        OutputStream outputStream = response.getOutputStream();
        try {
            String fileName = URLEncoder.encode("患者列表", "UTF-8");

            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" +fileName+ ".xls");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            //实例化 ExcelWriter
            writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLS, true);
            //实例化表单
            Sheet sheet = new Sheet(1, 0, PatientParems.class);
            sheet.setSheetName("目录");
            PagesBean<PatientParems> pagesBean=
                    adminPatientService.getListPatient(page,size,name,sex,city,hasRecord,diagnosis_time,doctorId);
            List<PatientParems> data = pagesBean.getData();
            //输出
            writer.write(data, sheet);
            writer.finish();
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @ApiOperation(value = "后端导出医生列表",httpMethod = "get")
    @RequestMapping(value = "/exporDoctorList",method = RequestMethod.GET)
    public void exporDoctorList(HttpServletResponse response,
                                String doctorName,
                                Integer hospitalId,
                                Integer doctorStart,
                                String startTime,
                                String endTime,
                                Integer doctorSex,
                                @NotNull(message = "currentPage不能为空") Integer currentPage,
                                @NotNull(message = "limit不能为空") Integer limit) throws IOException{
        ExcelWriter writer = null;
        OutputStream outputStream = response.getOutputStream();
        try {
            String fileName = URLEncoder.encode("医生列表", "UTF-8");
            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" +fileName +".xls");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            //实例化 ExcelWriter
            writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLS, true);
            //实例化表单
            Sheet sheet = new Sheet(1, 0, ResultDoctor.class);
            sheet.setSheetName("目录");
            List<ResultDoctor> list =doctorService.selectDoctor(doctorName,hospitalId,doctorStart,startTime,endTime,doctorSex,currentPage,limit);

            //输出
            writer.write(list, sheet);
            writer.finish();
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
